﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace GameWP
{
    public abstract class GameScreen : Microsoft.Xna.Framework.DrawableGameComponent
    {
#region Properties
        List<GameComponent> components = new List<GameComponent>();
        protected Game _game;
        protected MainGame _mainGame;
        protected SpriteBatch _spriteBatch;
        public List<GameComponent> Components
        {
            get { return components; }
        }
#endregion
        
#region  Initilation
        public GameScreen(Game game, MainGame mainGame, SpriteBatch spriteBatch)
            : base(game)
        {
            this._game = game;
            _mainGame = mainGame;
            this._spriteBatch = spriteBatch;
        }

        public override void Initialize()
        {
            base.Initialize();
        }
#endregion

#region  Delegate + event
        // Y nghia: Khi gamescreen nay duoc kich hoat ( show ) thi cac game screen khac phai hide di
        public delegate void IsShowed(int index);
        public event IsShowed Showed;
        /*
        * gamescreen.Showed += new GameScreen.IsShow(FunctionIsCalled);         
        */

#endregion
         
#region Methods

        public virtual void Show(int id)
        {
            this.Visible = true;
            this.Enabled = true;
            foreach (GameComponent component in components)
            {
                component.Enabled = true;
                if (component is DrawableGameComponent)
                    ((DrawableGameComponent)component).Visible = true;
            }
            if (Showed != null)
                Showed(id);
        }

        public virtual void Hide()
        {
            this.Visible = false;
            this.Enabled = false;
            foreach (GameComponent component in components)
            {
                component.Enabled = false;
                if (component is DrawableGameComponent)
                    ((DrawableGameComponent)component).Visible = false;
            }
        }

        public virtual void ResetScreen(int input)        {        }
#endregion

#region Update & Draw
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            //foreach (GameComponent component in components)
            for (int i = 0; i < components.Count; i++) 
                if (components[i].Enabled == true)
                    components[i].Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            foreach (GameComponent component in components)
                if (component is DrawableGameComponent &&
                    ((DrawableGameComponent)component).Visible)
                    ((DrawableGameComponent)component).Draw(gameTime);
        }
        #endregion       
 
    }
}
